package com.classic.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.TextView;

import com.classic.duanzhi.R;
import com.classic.duanzhi.TabModel;

import java.util.ArrayList;
import java.util.List;

public class SelectLabelDialog extends Dialog {

    private DialogAdapter adapter;
    private int index = 0;
    private List<TabModel> data = new ArrayList<>();
    private OnItemClickListener listener;
    private Context context;

    public SelectLabelDialog(@NonNull Context context, List<TabModel> data, OnItemClickListener listener) {
        super(context, R.style.dialog);
        this.data = data;
        this.listener = listener;
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.ad_dialog_select_layout);
        GridView view = findViewById(R.id.gv_view);
        adapter = new DialogAdapter();
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
        lp.height = (int) (d.heightPixels * 0.7); // 高度设置为屏幕的0.6
        dialogWindow.setAttributes(lp);

        view.setAdapter(adapter);
        adapter.setData(data);
        view.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (null != listener) listener.onClick(position);
                index = position;
                adapter.notifyDataSetChanged();
                dismiss();
            }
        });
    }

    public interface OnItemClickListener {
        void onClick(int index);
    }

    class DialogAdapter extends BaseAdapter {
        private List<TabModel> mData = new ArrayList<>();

        @Override
        public int getCount() {
            return mData.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @SuppressLint("InflateParams")
        @Override
        public View getView(int position, View v, ViewGroup parent) {
            ViewHolder holder;
            if (null == v) {
                holder = new ViewHolder();
                v = getLayoutInflater().inflate(R.layout.ad_item_dialog_layout, null);
                holder.title = v.findViewById(R.id.tv_dialog_title);
                v.setTag(holder);
            } else {
                holder = (ViewHolder) v.getTag();
            }
            holder.title.setText(mData.get(position).getTitle());
            holder.title.setSelected(position == index);
            return v;
        }

        public void setData(List<TabModel> data) {
            this.mData = data;
            notifyDataSetChanged();
        }
    }

    public void show(int p) {
        this.index = p;
        if (null != adapter) {
            adapter.notifyDataSetChanged();
        }
        show();
    }

    public void initPosition(int p) {
        this.index = p;
        if (null != adapter) {
            adapter.notifyDataSetChanged();
        }
    }

    class ViewHolder {
        TextView title;
    }

}
